using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Mpi.Common.Graphic
{
    public  class GraphicEffect
    {
        public static void DrawSmoothBackGround(Graphics g, RectangleF rect, Color baseColor, float transparencyOffset, float smoothnes, float heightCor, float widthCor)
        {

            RectangleF workRect = new RectangleF(rect.X + widthCor / 2, rect.Y + heightCor / 2, rect.Width - widthCor, rect.Height - heightCor);


            float x = workRect.Left - smoothnes / 2;
            float y = (workRect.Height / 2f + workRect.Top);
            float lenght = workRect.Width + smoothnes;
            float transpStep = (255 - transparencyOffset) / smoothnes;
            float thickness = workRect.Height + 2 * smoothnes;

            //RectangleF r = new RectangleF(x, y, lenght, rect.Height - (heightCor / 2));

            for (int i = 0; i <= smoothnes; i++)
            {
                using (Brush b = new SolidBrush(Color.FromArgb((int)((transpStep) * 1.2), baseColor)))
                {
                    using (Pen p = new Pen(b, thickness - 2 * i))
                    {
                        p.EndCap = LineCap.Round;
                        p.StartCap = LineCap.Round;
                        g.DrawLine(p, x + i, y, x + lenght - i, y);
                    }
                }

            }
            //g.DrawRectangle(Pens.Red, Rectangle.Round(workRect));
            //g.DrawRectangle(Pens.Blue, Rectangle.Round(rect));
        }

        public static void DrawHighLightedBackground(Graphics g, GraphicsPath gp, Color centerColor, Color surroundingColor)
        {
            PathGradientBrush pgb = new PathGradientBrush(gp.PathPoints);
            Color[] c ={ (surroundingColor) };
            pgb.CenterColor = centerColor;
            pgb.SurroundColors = c;
            g.FillPath(pgb, gp);
        }



        public static void DrawGlassRectangle(Graphics g, RectangleF rect,Color glassColor,int coloringStrange, float topQuantil, bool border)
        {
            if (rect.IsEmpty)
                return;

            RectangleF top = new RectangleF();
            top.X = rect.X;
            top.Y = rect.Y;
            top.Width = rect.Width;
            top.Height = rect.Height * topQuantil;
            RectangleF top2 = new RectangleF();
            top2.X = rect.X;
            top2.Y = rect.Y;
            top2.Width = rect.Width;
            top2.Height = (rect.Height * topQuantil) + 1f;

           // p = new Pen(Color.FromArgb(180, Color.White), 1);
            using (LinearGradientBrush lgb = new LinearGradientBrush(top2, Color.FromArgb(60, Color.White), Color.FromArgb(255, Color.White), 270f))
            {
                using (Pen borderPen = new Pen(glassColor))
                {
                    lgb.SetSigmaBellShape(1f);
                    g.FillRectangle(new SolidBrush(Color.FromArgb(coloringStrange, glassColor)), rect);
                    g.FillRectangle(lgb, top);
                    if (border)
                    {
                        g.DrawLine(borderPen, rect.Left, rect.Top, rect.Right, rect.Top);
                        g.DrawLine(borderPen, rect.Left, rect.Bottom, rect.Right, rect.Bottom);
                    }
                }
                //g.DrawRectangles(Pens.White, new RectangleF[1] { rect });
            }

        }

        public static void DrawEllipticGlow(Graphics g, RectangleF rect, Color glowColor, Color glowCenterColor, float centerWeight)
        {

            using (GraphicsPath path = new GraphicsPath())
            {
                using (GraphicsPath pathCenter = new GraphicsPath())
                {
                    RectangleF brushMainRect = new RectangleF(rect.Left, rect.Top, rect.Width, rect.Height * 2f);
                    path.AddEllipse(brushMainRect);
                    float h = brushMainRect.Height * centerWeight;
                    RectangleF brushCenterRect = new RectangleF(brushMainRect.Left, brushMainRect.Top + ((brushMainRect.Height-h)/ 2f), brushMainRect.Width, h);
                    pathCenter.AddEllipse(brushCenterRect);

                    using (PathGradientBrush pgb = new System.Drawing.Drawing2D.PathGradientBrush(path.PathPoints))
                    {
                        Color[] c ={ (Color.Transparent) };
                        pgb.CenterColor = Color.FromArgb(255, glowColor);
                        pgb.SurroundColors = c;
                        pgb.WrapMode = WrapMode.Clamp;
                        g.FillRectangle(pgb, rect);
                        //g.DrawPath(Pens.Red, path);
                        using (PathGradientBrush pgb2 = new System.Drawing.Drawing2D.PathGradientBrush(pathCenter.PathPoints))
                        {
                            pgb2.CenterColor = glowCenterColor;
                            pgb2.SurroundColors = c;
                            pgb2.WrapMode = WrapMode.Clamp;
                            //g.DrawPath(Pens.Black, pathCenter);
                            g.FillRectangle(pgb2, rect);
                        }
                    }
                }
            }
        }
        
        /// <summary>
        /// Draws a rectangle with rounded edges
        /// </summary>
        /// <param name="X">The x coordinate.</param>
        /// <param name="Y">the y coordinate.</param>
        /// <param name="width">The width of the rectangle.</param>
        /// <param name="height">The height of thze rectangle.</param>
        /// <param name="radius">The radius oft the edges.</param>
        public static GraphicsPath RoundRect(float X, float Y, float width, float height, float radius)
        {
            GraphicsPath gp = new GraphicsPath();

            if (radius > 0)
            {
                gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
                gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
                gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
                gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
                gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);

                gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);

                gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);

                gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);

                gp.CloseFigure();
            }
            else
            {

                gp.AddRectangle(new RectangleF(X, Y, width, height));
            }

            return gp;
        }

        /// <summary>
        /// Draws a rectangle with rounded edges
        /// </summary>
        /// <param name="rect">The rectangle.</param>
        /// <param name="radius">The radius oft the edges.</param>
        public static GraphicsPath RoundRect(RectangleF rect, float radius)
        {
            return RoundRect(rect.X, rect.Y, rect.Width, rect.Height, radius);
        }
    }
}
